package com.zzq.guli.service.edu.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zzq.guli.service.base.model.TeacherQuery;
import com.zzq.guli.service.base.result.R;
import com.zzq.guli.service.edu.entity.Course;
import com.zzq.guli.service.edu.entity.Teacher;
import com.zzq.guli.service.edu.mapper.CourseMapper;
import com.zzq.guli.service.edu.service.CourseService;
import com.zzq.guli.service.edu.service.TeacherService;
import com.zzq.guli.service.feign.oss.OssFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ZZQ
 * @since 2024-04-12
 */
//@CrossOrigin
@Api("讲师接口")
@RestController//教师接口
@RequestMapping("/api/edu/teacher")

public class ApiTeacherController {


    @Autowired
    TeacherService teacherService;



    @Autowired
    CourseMapper courseMapper;


    @Autowired
    OssFeignClient ossFeignClient;

    @ApiOperation(value = "查询讲师信息",notes = "查询")
    //查询老师列表信息
    @GetMapping("/getAll")
    public R listAll(@RequestHeader("token") String token){

        System.out.println(token);
        System.out.println("查询讲师列表");
        List<Teacher> teachers=teacherService.list();
        return R.ok().data("teachers",teachers);


    }


    @ApiOperation(value ="更具id查询信息" )
    @GetMapping("/getTeacherById/{id}")
    //根据讲师id查询讲师信息
    public R getTeacherById(@PathVariable String id){
        Teacher byId = teacherService.getById(id);
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("teacher_id",id);
        List<Course> courses=courseMapper.selectList(courseQueryWrapper);
        return R.ok().data("teacher",byId).data("courseList",courses);
    }


    @ApiOperation(value = "删除讲师信息",notes = "根据id删除讲师信息")
    //删除讲师信息
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(name = "id", readOnly = true) @PathVariable String id){
        Teacher byId = teacherService.getById(id);
        boolean b = teacherService.removeById(id);
        if(b){
            String avatar = byId.getAvatar();
            R r = ossFeignClient.fileDelete(avatar);

            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除讲师信息失败");
        }

    }


    @ApiOperation(value ="查找信息" )
    @PostMapping("/selectTeacher")
    //根据讲师id更新讲师信息
    public R selectTeacherById(@RequestBody Teacher teacher){
        if(StringUtils.isEmpty(teacher.getId())){
            teacherService.updateById(teacher);
            return R.ok().message("查询讲师信息成功");
        }else {
            return R.error().message("查寻讲师信息失败");
        }

    }


    @ApiOperation(value = "分页查询")
    //分页查询
    @GetMapping("/select/{page}/{limit}")
    public R selectPage(
            @ApiParam(name = "当前页数",readOnly = true) @PathVariable Integer page,
            @ApiParam(name = "要查的条数",readOnly = true) @PathVariable Integer limit,
            TeacherQuery teacherQuery
    ){

        IPage<Teacher> page1=teacherService.selectPage(page,limit,teacherQuery);

        return R.ok().data("teacherPage",page1);


    }


    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    //新增讲师信息
    public R save(@ApiParam(name = "讲师") @RequestBody Teacher teacher) {

        boolean save = teacherService.save(teacher);

        if (save) {
            return R.ok().message("添加讲师成功");
        } else {
            return R.error().message("添加讲师失败");
        }
    }


        @PostMapping("/updateTeaacher")
        //新增讲师信息
        public R uppdateTeacher(@ApiParam(name = "讲师")  @RequestBody Teacher teacher) {
            boolean b = teacherService.updateById(teacher);

            if (b) {
                return R.ok().message("更新讲师成功");
            } else {
                return R.error().message("更新讲师失败");
            }


        }

}


